* ---- Does Cognitive Dissonance Add anything to RU models of Barebris & Xiong ------

* Generate lagged CDS spreads
gen lag_cds_spread = L.cds if _n > 1
bysort firm (trend): replace lag_cds_spread = L.cds

* Calculate CDS returns (percentage change in spreads)
gen cds_return1 = (cds - lag_cds_spread) / lag_cds_spread

* ----------- Create Realization Utility Variables -------------
* Create realized gains/losses as Barberis and Xiong's realization utility mechanism
gen realization_gain = cds - lag_cds_spread

* Create a binary indicator for gains/losses
gen gain = (realization_gain > 0)

* Create interaction terms for realization utility
gen gain_realization = gain * realization_gain

* Realization utility model
reg cds_return1 gain_realization liquidity equity_return leverage term i.firm i.trend
predict fitted_realization, xb
gen predicted_cds_spread_realization = lag_cds_spread * (1 + fitted_realization)
gen deviation_realization = predicted_cds_spread_realization - cds

* Create cognitive dissonance proxy: deviation between expected and observed spreads
* gen cognitive_dissonance = abs(predicted_cds_spread_realization - cds)
* --- or use sentiment alrteady defined as RJV+CoJumps; or use RJV 
gen cognitive_dissonance=sentiment
* Full model with cognitive dissonance
reg cds_return1 gain_realization cognitive_dissonance liquidity equity_return leverage term i.firm i.trend
predict fitted_cognitive, xb
gen predicted_cds_spread_cognitive = lag_cds_spread * (1 + fitted_cognitive)
gen deviation_cognitive = predicted_cds_spread_cognitive - cds

* LR Test
lrtest fitted_cognitive fitted_realization
* Goodness-of-fit comparison
estat ic, eform  // AIC and BIC for model comparison

* Compare R-squared values
display "Realization Utility Model R-squared: " e(r2)
display "Cognitive Dissonance Model R-squared: " e(r2_a)

* ----------- Test the incremental contribution -----------------------
* Model with realization utility only
reg cds_return1 gain_realization liquidity equity_return leverage sp500 term i.firm i.trend

* Full model with cognitive dissonance
reg cds_return1 gain_realization cognitive_dissonance liquidity equity_return leverage sp500 term i.firm i.trend

* Perform an F-test to check for significant improvement
test cognitive_dissonance

* --------- Analyze Deviations and Trading Volume ---------------
* Trading volume regression with realization utility
reg volume L(1/3).deviation_realization  leverage term

* Trading volume regression with cognitive dissonance
reg volume L(1/3).deviation_cognitive  leverage term

* Compare R-squared values
display "Realization Utility Model R-squared (trading volume): " e(r2)
display "Cognitive Dissonance Model R-squared (trading volume): " e(r2_a)

* ---- Robustness Checks ------
* Granger causality for realization utility vs. cognitive dissonance
pvar cds_return1 gain_realization cognitive_dissonance, lags(3)
vargranger

* Split data into training (80%) and testing (20%) sets
gen train = (uniform() < 0.8)
reg cds_return1 gain_realization liquidity equity_return leverage sp500 term if train
predict fitted_train_realization if train
predict fitted_test_realization if !train

reg cds_return1 gain_realization cognitive_dissonance liquidity equity_return leverage sp500 term if train
predict fitted_test_cognitive if !train

* Perform an F-test to check for significant improvement
test cognitive_dissonance
* Calculate RMSE for both models
gen residual_realization = cds_return1 - fitted_test_realization
gen residual_cognitive = cds_return1 - fitted_test_cognitive
summ residual_realization, detail
summ residual_cognitive, detail

* --------------- Visualize Model Comparisons -----------------------
gen t=tm(2010m1)+_n-1
format t %tm
* Plot deviations for both models
twoway (line deviation_realization t if firm == 1, lcolor(blue)) ///
       (line deviation_cognitive t if firm == 1, lcolor(red)), ///
       title("Model Comparison: Deviations for Firm 1") ///
       legend(label(1 "Realization Utility") label(2 "Sentiment"))
* --------------- Model Fit ------------------------
* Plot predicted vs. observed spreads
scatter cds predicted_cds_spread_realization if firm == 1, mcolor(blue) ///
       title("Predicted vs. Observed (Realization Utility)")
scatter cds predicted_cds_spread_cognitive if firm == 1, mcolor(red) ///
       title("Predicted vs. Observed (Sentiment)")

* ------------------ Liklihood Ratio Test *NOT USED*-----------------------------
*Step 1: Estimate the full model and save results 
logit dependent_var independent_vars belief1 belief2
estimates store full_model
* regress, xtreg, or your actual estimation command)
* Step 2: Estimate the restricted model (drop belief parameters)
logit dependent_var independent_vars
estimates store restricted_model
* Step 3: Perform the likelihood ratio test
lrtest restricted_model full_model